/*!
\file ContainerElement.cpp
\brief Contains implementation of ContainerElement.
\ingroup Algorithm
*/

#include"ContainerElement.h" 

ContainerElement::ContainerElement(){
	m_node = 0;
	m_edge = 0;
	m_dataInt = 0;
	m_dataDouble = 0;
	m_intUsed = false;
	m_doubleUsed = false;
	m_stringUsed = false;
}

ContainerElement::~ContainerElement(){
	if( m_node != 0 )
		delete m_node;
	if( m_edge != 0 )
		delete m_edge;
}

ContainerElement *ContainerElement::setNode( const Node &node ){
	if( m_node != 0 ){
		_report( new ContainerElementRemoveNodeAction( 0, 0, m_node->identifier(), m_node->label() ) );
		delete m_node;
	}
	m_node = new Node( node );
	_report( new ContainerElementSetNodeAction(0, 0, node.identifier(), node.label()) );
	return this;
}

ContainerElement *ContainerElement::setEdge( const Edge &edge ){
	if( m_edge != 0 ){
		_report( new ContainerElementRemoveEdgeAction( 0, 0, m_edge->identifier(), m_edge->from().identifier(), m_edge->to().identifier(), m_edge->label() ) );
		delete m_edge;
	}
	m_edge = new Edge( edge );
	_report( new ContainerElementSetEdgeAction( 0, 0, edge.identifier(), edge.from().identifier(), edge.to().identifier(), edge.label() ) );
	return this;
}

ContainerElement *ContainerElement::setData( int data ){
	m_dataInt = data;
	m_intUsed = true;
// 	_report( new ContainerElementSetDataAction(0, 0, QString().setNum(data)) );
	_reportChangedData();
	return this;
}

ContainerElement *ContainerElement::setData( double data ){
	m_dataDouble = data;
	m_doubleUsed = true;
// 	_report( new ContainerElementSetDataAction(0, 0, QString().setNum(data)) );
	_reportChangedData();
	return this;
}

ContainerElement *ContainerElement::setData( QString data ){
	m_dataString = data;
	m_stringUsed = true;
// 	_report( new ContainerElementSetDataAction(0, 0, data) );
	_reportChangedData();
	return this;
}

void ContainerElement::_reportChangedData(){
	bool used = false;
	QString data;
	if( intUsed() )
		data./*append("Data ").*/append(QString().setNum(m_dataInt));
	
	if( doubleUsed() )
		if( data.isEmpty() )
			data./*append("Data ").*/append(QString().setNum(m_dataDouble));
		else
			data.append("\n").append(QString().setNum(m_dataDouble));
	
	if( stringUsed() )
		if( data.isEmpty() )
			data/*.append("Data ")*/.append(m_dataString);
		else
			data.append("\n").append(m_dataString);
	_report( new ContainerElementSetDataAction(0, 0, data) );
}

const Node *ContainerElement::node()const{
	return m_node;
}

const Edge *ContainerElement::edge()const{
	return m_edge;
}

int ContainerElement::dataInt()const{
	return m_dataInt;
}

double ContainerElement::dataDouble()const{
	return m_dataDouble;
}

QString ContainerElement::dataString()const{
	return m_dataString;
}

void ContainerElement::_report( ContainerElementAction *action ){
	if( receivers(SIGNAL(reportAction(ContainerElementAction*))) > 0 ){
		emit reportAction(action);
	}else{
		delete action;
	}
}